package com.baozhun.mall.common.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import android.webkit.WebSettings
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.R
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.databinding.ActivityWebContainerBinding
import com.baozhun.mall.common.util.CacheUtil
import com.baozhun.mall.common.util.Constants
import com.blankj.utilcode.util.ToastUtils
import com.github.lzyzsd.jsbridge.BridgeHandler
import com.tencent.sonic.sdk.SonicSessionConfig
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import org.json.JSONObject

@Route(path = RoutePath.Common.WEB_CONTAINER)
class WebContainerActivity : BaseActivity<BaseViewModel, ActivityWebContainerBinding>() {
    var url: String? = null
    val mTitle by lazy { intent.getStringExtra(Constants.Common.TITLE) }


    companion object {
        fun start(context: Context, url: String, title: String) {
            val intent = Intent(context, WebContainerActivity::class.java).apply {
                putExtra(Constants.Common.BUNDLE_NAME, url)
                putExtra(Constants.Common.TITLE, title)
            }
            context.startActivity(intent)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        url = intent.getStringExtra(Constants.Common.BUNDLE_NAME)
        if (TextUtils.isEmpty(url)) {
            finish()
            return
        } else {
            url = addParams(url!!)
        }
        mDataBinding.titleBar.title = mTitle
        mDataBinding.titleBar.setBackgroundColor(ContextCompat.getColor(this, com.baozhun.mall.res.R.color.white))
        window.addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)
        val sessionConfigBuild = SonicSessionConfig.Builder()
        sessionConfigBuild.setSupportLocalServer(true)
        val webSetting = mDataBinding.webViewContainerLayout.settings
        webSetting.javaScriptEnabled = true
        webSetting.allowContentAccess = true
        webSetting.databaseEnabled = true
        webSetting.domStorageEnabled = true
        webSetting.cacheMode = WebSettings.LOAD_DEFAULT
        webSetting.useWideViewPort = true
        webSetting.loadWithOverviewMode = true
        mDataBinding.webViewContainerLayout.registerHandler("startGoodsDetail", BridgeHandler { data, function ->
            try {

                ARouter.getInstance().build(RoutePath.Home.GOODS_DETAIL)
                    .withString(
                        Constants.Goods.GOODS_ID, JSONObject(data).getString("goodsId")
                    )
                    .withString(Constants.Goods.GOODS_SOURCE, JSONObject(data).getString("source"))
                    .navigation()
                function.onCallBack("success")
            } catch (e: Exception) {
                // ignore
            }
        })

        mDataBinding.webViewContainerLayout.registerHandler("startLogin", BridgeHandler { data, function ->
            try {
                if (CacheUtil.isLogin()) {
                    function.onCallBack(CacheUtil.getUser().token)
                } else {
                    ARouter.getInstance().build(RoutePath.Me.LOGIN).navigation()
                }
            } catch (e: Exception) {
                // ignore
            }
        })

    }

    private fun addParams(url: String): String {
        var urlWithParams = ""
        urlWithParams = if (url.contains("?")) {
            "$url&isApp=1&platform=android&accessToken=" + CacheUtil.getUser().token
        } else {
            "$url?isApp=1&platform=android&accessToken=" + CacheUtil.getUser().token
        }
        return urlWithParams
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {

        }
    }
}